import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, ValidationErrors, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute, Router } from '@angular/router';  // 用于获取路由参数
import { DepartmentInfo } from 'src/app/models';
import { DepartmentService } from 'src/app/services/department.service';
import { OrganizationService } from 'src/app/services/organization.service';
import { ExceptionHandler } from 'src/app/handler/exception.handler';
@Component({
  selector: 'zepride-edit-department',
  templateUrl: './edit-department.component.html',
  styleUrls: ['./edit-department.component.less']
})
export class EditDepartmentComponent implements OnInit {
  validateForm: FormGroup;
  departmentInfo: DepartmentInfo = {} as DepartmentInfo;
  id: string;
  parentId: string;
  notequId: string;
  listOfOption = [{ label: '行政部门', value: '行政部门' }, { label: '审核部门', value: '审核部门' }];
  departmentList = []; //上级部门列表
  UserList = []; //人员列表
  logining = false;

  constructor(
    private fb: FormBuilder,
    private msg: NzMessageService,
    private route: Router,
    private activatedRoute: ActivatedRoute,
    private departmentService: DepartmentService,
    private organizationService: OrganizationService,
  ) {
    this.validateForm = this.fb.group({
      name: ['', [Validators.required]],
      depType: ['', [Validators.required]],
      leaderPerson: [null],
      description: [null],
      parentId: ['', [Validators.required]],
    });
    //获取编辑需要的参数
    activatedRoute.queryParams.subscribe(queryParams => {
      this.id = queryParams.Id;
      this.parentId = queryParams.parentId;
      this.notequId = queryParams.notequId;
      this.departmentInfo.organizationId = queryParams.organizationId;
    });
  }

  ngOnInit(): void {
    if (this.id != '0') {
      this.getDepartmentInfoById();
    } else {
      this.departmentInfo.parentId = this.parentId;
      this.departmentInfo.depType = '行政部门';
    }

    this.getDepartment();
    this.getUser();
  }
  //树
  getOrganizationTrees(objectName) {
    return this.organizationService.getOrganizationTrees({ objectName: objectName, parentId: '', Organization: '', Department: '', User: '' });
  }

  //查询上级部门
  async getDepartment() {
    this.departmentList = await this.getOrganizationTrees(['Department', 'Organization']);
  }
  //查询人员
  async getUser() {
    this.UserList = await this.getOrganizationTrees('User');
  }
  //查询详情
  async getDepartmentInfoById() {
    try {
      const data = await this.departmentService.getDepartmentInfoById(this.id);
      this.departmentInfo = data;
      this.departmentInfo.parentId = this.parentId;
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }
  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }
  }
  resetForm(e: MouseEvent): void {
    e.preventDefault();
    this.validateForm.reset();
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsPristine();
      this.validateForm.controls[key].updateValueAndValidity();
    }
  }
  goBack() {
    history.go(-1);
  }
  async confirm() {
    if (!this.validateForm.valid) return;
    if (this.logining) {
      return;
    }
    this.logining = true;
    try {
      this.departmentInfo.orgTree = { parentId: this.departmentInfo.parentId };
      this.departmentInfo.status = '0'; //默认状态 正常
      if (this.id != '0') {
        const data = await this.departmentService.updateDepartment(this.departmentInfo);
      } else {
        const data = await this.departmentService.createDepartment(this.departmentInfo);
      }
      this.msg.success(this.id != '0' ? '编辑部门成功' : '添加部门成功');
      history.go(-1);
    } catch (error) {
      ExceptionHandler.current.handleError(error);

    } finally {
      this.logining = false;
    }
  }
}
